var React = require('react');
require("./source/default/_css/umeditor.css");
var Api = require("../../business_platform/api/api.js");

var Editor = React.createClass({
    componentDidMount:function(){
        var _this = this;
        var editor = UM.getEditor(_this.props.id, {
            //图片上传配置区
            imageUrl: _this.props.uplaodUrl || Api.uploadImageByEditor,             //图片上传提交地址
            //imagePath:URL + "php/",                     //图片修正地址，引用了fixedImagePath,如有特殊需求，可自行配置
            //imageFieldName:"upfile",                   //图片数据的key,若此处修改，需要在后台对应文件修改对应参数

            toolbar:[
                'source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat emotion |',
                'insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize' ,
                '| justifyleft justifycenter justifyright justifyjustify |',
                'link unlink | image',
                '| horizontal',
            ],
            lang:"zh-cn"
            //字体
            ,'fontfamily':[
                { label:'',name:'songti',val:'宋体,SimSun'},
                { label:'',name:'kaiti',val:'楷体,楷体_GB2312, SimKai'},
                { label:'',name:'yahei',val:'微软雅黑,Microsoft YaHei'},
                { label:'',name:'heiti',val:'黑体, SimHei'},
                { label:'',name:'lishu',val:'隶书, SimLi'},
                { label:'',name:'andaleMono',val:'andale mono'},
                { label:'',name:'arial',val:'arial, helvetica,sans-serif'},
                { label:'',name:'arialBlack',val:'arial black,avant garde'},
                { label:'',name:'comicSansMs',val:'comic sans ms'},
                { label:'',name:'impact',val:'impact,chicago'},
                { label:'',name:'timesNewRoman',val:'times new roman'}
            ]
            //字号
            ,'fontsize':[10, 11, 12, 14, 16, 18, 20, 24, 36]
            , enableAutoSave : false
            //, initialFrameHeight: _this.props.height
            , initialFrameWidth: '100%'
            ,readonly:_this.props.disabled
        });
        window.setTimeout(function(){
            var {value} = _this.props;
            if(value){
                editor.setContent(UM.utils.html(value));
            }
            $(".edui-container .edui-editor-body")
                .css({
                    overflow: "hidden",
                    overflowY: "auto",
                    height: _this.props.height
                });
        },2000);
        _this.setState({
            editor:editor
        })
    },
    componentWillUnmount:function(){
        var editor  = this.state ? this.state.editor : null;
        if(editor){
            editor.destroy();
        }
        console.log("at this scope destroy editor, because umEditor can't init twice !");
        this.setState({
            editor:null
        })
    },
    render : function(){
        return (<script id={this.props.id} name="content" type="text/plain"></script>)
    }
});


module.exports = Editor;